﻿using System.Web;

namespace CH.FourBoards.LagerWalti.FrontEnd.Util
{
	public static class HttpUtil
	{
		static public string ServerRoot
		{
			get
			{
				var scheme = HttpContext.Current.Request.Url.Scheme;
				var host = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
				var path = HttpContext.Current.Request.ApplicationPath ?? string.Empty;
				path = path.Equals("/") ? string.Empty : path;
				return string.Format("{0}://{1}{2}", scheme, host, path);
			}
		}

		public static string ApplicationRoot
		{
			get
			{
				var ret = HttpContext.Current.Request.ApplicationPath;
				ret = ret ?? string.Empty;
				if (!ret.EndsWith("/")) {
					ret += "/";
				}
				return ret;
			}
		}

		public static string MappedApplicationPath
		{
			get
			{
				var appPath = HttpContext.Current.Request.ApplicationPath ?? string.Empty;
				appPath = appPath.ToLower();

				// is site?
				if (appPath == "/") {
					appPath = "/";
				}

				// is virtual?
				else if (!appPath.EndsWith(@"/")) {

					appPath += @"/";
				}

				var it = HttpContext.Current.Server.MapPath(appPath);
				if (!it.EndsWith(@"\")) {
					it += @"\";
				}
				return it;
			}
		}
	}
}